package ACWing.SearchAndGraphTheory.DFS;
//184. 虫食算
/**
 * 思路：暴力搜索加剪枝
 * 1先从个位将字母出现的顺序展示出来
 * 2按字母出现的先后顺序进行暴力搜索
 * 3剪枝将已经判断出的值进行N进制加法因为是加法所以进位只能是0或者1
 *
 *
 */

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2022/12/21 14:19
 */
public class InsectFeedingCalculation {
    static int n;
    static char[][]e=new char[3][30];
    static boolean[]st=new boolean[30];
    static int[]qu=new int[30];
    static int[]path=new int[30];
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        sc.nextLine();
        String s=sc.nextLine();
        e[0] = s.toCharArray();
        String s2=sc.nextLine();
        e[1] = s2.toCharArray();
        String s3=sc.nextLine();
        e[2] = s3.toCharArray();
        for (int i = n-1,k=0 ;i>=0; i--) {
            for (int j = 0; j < 3; j++) {
                int t=e[j][i]-'A';
                if(!st[t]){
                    qu[k++]=t;
                    st[t]=true;
                }
            }

        }
        Arrays.fill(st,false);
        Arrays.fill(path,-1);
        dfs(0);
        for (int i = 0; i < n; i++) {
            System.out.print(path[i]+" ");
        }
    }

    private  static boolean  dfs(int u) {
        if(u==n){
            return true;
        }
        for (int i = 0; i < n; i++) {
            if(!st[i]){
                path[qu[u]]=i;
                st[i]=true;
                if(check()&&dfs(u+1)){
                    return true;
                }
                path[qu[u]]=-1;
                st[i]=false;
            }
        }

        return false;
    }

    private static boolean check() {
        for (int i = n-1, t=0;i >= 0; i--) {
            int a=path[e[0][i]-'A'];
            int b=path[e[1][i]-'A'];
            int c=path[e[2][i]-'A'];
            if(a!=-1&&b!=-1&&c!=-1){
                if(t==-1) {
                    if ((a + b + 0)%n != c && (a + b + 1)%n != c) {
                        return false;
                    }
                    if(i==0&&a+b>=n){
                        return false;
                    }
                }else {
                    if((a+b+t)%n!=c){
                        return false;
                    }
                    if(i==0&&a+b>=n){
                        return false;
                    }
                    t=(a+b+t)/n;
                }
            }else {
                t=-1;
            }
        }
        return true;
    }
}

